package sat.component;

import org.mt4j.MTApplication;
import org.mt4j.components.MTComponent;
import org.mt4j.components.visibleComponents.shapes.MTRectangle;
import org.mt4j.components.visibleComponents.widgets.buttons.MTImageButton;
import org.mt4j.input.inputProcessors.IGestureEventListener;
import org.mt4j.input.inputProcessors.MTGestureEvent;
import org.mt4j.input.inputProcessors.componentProcessors.tapProcessor.TapEvent;
import org.mt4j.input.inputProcessors.componentProcessors.tapProcessor.TapProcessor;
import org.mt4j.util.MTColor;
import processing.core.PImage;

public class KeyboardTabImage extends MTRectangle {
	private MTApplication app;
	private Keyboard keyboard;
	private MTImageButton tabImageButton;
	private final MTColor vert = new MTColor(122, 142, 117, 255);
	private final MTColor marron = new MTColor(164, 117, 91, 255);
	
	//when being selected
	public void outLine(){
		//this.setStrokeColor(MTColor.RED);
		tabImageButton.setFillColor(marron);
		tabImageButton.setStrokeColor(vert);
	}
	
	public void inLine(){
		//this.setFillColor(MTColor.WHITE);
		tabImageButton.setFillColor(MTColor.WHITE);
		tabImageButton.setStrokeColor(vert);
	}
	
	public KeyboardTabImage(MTApplication mtApplication,Keyboard kb,float width, float height, String path) {
		super(mtApplication, 0, 0, 0, width, height);
		this.app = mtApplication;
		this.keyboard = kb;
		this.setFillColor(MTColor.WHITE);
		this.setStrokeColor(MTColor.BLACK);
		this.setStrokeWeight(6);
		this.unregisterAllInputProcessors(); 	//Remove the default drag, rotate and scale gestures first
	
		//add corresponding image to button
		PImage buttonImage=app.loadImage(path);
		buttonImage.resize((int)width, (int)height);	//Resize the image to the size of tabImage
		tabImageButton= new MTImageButton(app, buttonImage);
		
		// implement gesture for imageButton 
		tabImageButton.unregisterAllInputProcessors();
		tabImageButton.registerInputProcessor(new TapProcessor(this.app));
		tabImageButton.addGestureListener(TapProcessor.class, new IGestureEventListener() {
			@Override
			public boolean processGestureEvent(MTGestureEvent ge) {
				// TODO Auto-generated method stub
				TapEvent te = (TapEvent)ge;
				MTComponent target = (MTComponent) te.getTarget();
				if (target instanceof MTImageButton) {
					switch (te.getTapID()) {
					case TapEvent.TAPPED:
							keyboard.getTabArrayImage().allInLine();
							//To do: change the type of the relation
							outLine();
						break;
					default:
						break;
					}
				}
				return false;
			}
		});

		this.addChild(tabImageButton);
	}
}
